<?php

/**
 * @author gbfun <gbfun@163.com>  2017-09-16 20:03
 * http://www.wuweiyou.com
 */

class db_table_background_good extends db_table_good
{    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    public function add($good_info)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
        
        $sql = '
            INSERT INTO ' . $this->_full_name . '
                (
                    `name`, `brief`, `description`, `after_sale_service`, `click`, `image`, 
                    `order`, `status`, `if_show_number_grade`, `create_time`, `update_time`, `cat_id`  
                )
            VALUES
                (
                    ?, ?, ?, ?, ?, ?, 
                    ?, ?, ?, ?, ?, ? 
                )
        ';
        
        $bind = array(
            htmlspecialchars($good_info['name']),
            htmlspecialchars($good_info['brief']),
            $good_info['description'],
            htmlspecialchars($good_info['after_sale_service']),
            $good_info['click'],
            $good_info['image'],
            
            $good_info['order'],
            $good_info['status'],
            $good_info['if_show_number_grade'],
            $good_info['create_time'],
            $good_info['update_time'],
            $good_info['cat_id'],
        );
        
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
        
        return $result;
    }
    
    public function update($good_info)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `name` = ?,
                `brief` = ?,
                `description` = ?,
                `after_sale_service` = ?,
                `image` = ?,
                
                `order` = ?,
                `status` = ?,
                `if_show_number_grade` = ?,
                `cat_id` = ?,

                `update_time` = ?
            WHERE `id` = ?
        ';
    
        $bind = array(
            htmlspecialchars($good_info['name']),
            htmlspecialchars($good_info['brief']),
            $good_info['description'],
            htmlspecialchars($good_info['after_sale_service']),
            $good_info['image'],
            
            $good_info['order'],
            $good_info['status'],
            $good_info['if_show_number_grade'],
            $good_info['cat_id'],
            
            $good_info['update_time'],
    
            $good_info['id'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function update_good_price($good_info)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            UPDATE ' . $this->_full_name . '
            SET
                `min_price` = ?, 
                `max_price` = ?
            WHERE `id` = ?
        ';
    
        $bind = array(
            $good_info['min_price'], 
            $good_info['max_price'], 
            
            $good_info['id'],            
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function add_good_attr($good_info, $good_attr)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            INSERT INTO `good_good_attr`
                (`good_id`, `name`, `value`, `order`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?, ?, ?)
        ';
    
        $bind = array(
            $good_info['id'],
            htmlspecialchars($good_attr['name']),
            htmlspecialchars($good_attr['value']),
            $good_attr['order'],
            $good_info['update_time'],
            $good_info['update_time']            
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function add_good_sattr($good_info, $good_sattr)
    {
        //var_dump($good_sattr); exit();
        
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            INSERT INTO `good_good_sattr`
                (`good_id`, `name`, `order`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?, ?)
        ';
    
        $bind = array(
            $good_info['id'],
            htmlspecialchars($good_sattr['name']),
            $good_sattr['order'],
            $good_info['update_time'],    //每次都是新建，所以，以商品【更新时间】为准
            $good_info['update_time']            
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function add_good_number_grade($good_info, $good_number_grade)
    {
        //var_dump($good_number_grade); exit();
    
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            INSERT INTO `good_good_number_grade`
                (`good_id`, `number_min`, `number_max`, `order`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?, ?, ?)
        ';
    
        $bind = array(
            $good_info['id'],
            $good_number_grade['number_min'],
            $good_number_grade['number_max'],
            $good_number_grade['order'],
            $good_info['update_time'],    //每次都是新建，所以，以商品【更新时间】为准
            $good_info['update_time']
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function add_good_spec($good_info, $good_spec)
    {
        //var_dump($good_spec); exit();
        
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            INSERT INTO `good_good_spec`
                (`good_id`, `order`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?)
        ';
    
        $bind = array(
            $good_info['id'],
            //$good_spec['price'],
            $good_spec['order'],
            $good_info['update_time'],
            $good_info['update_time'],
            //$good_spec['discount_price'],
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
    
        return $result;
    }  
    
    public function add_good_spec_sattr($good_info, $good_spec_sattr)
    {
        //var_dump($good_spec_sattr); exit();
    
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            INSERT INTO `good_good_spec_sattr`
                (`good_spec_id`, `good_sattr_id`, `value`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?, ?)
        ';
    
        $bind = array(
            $good_spec_sattr['good_spec_id'],
            $good_spec_sattr['good_sattr_id'],
            htmlspecialchars($good_spec_sattr['value']),
            $good_info['update_time'],
            $good_info['update_time']
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function add_good_spec_number_grade($good_info, $good_spec_number_grade)
    {
        //var_dump($good_spec_sattr); exit();
    
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $sql = '
            INSERT INTO `good_good_spec_number_grade`
                (`good_spec_id`, `good_number_grade_id`, `price`, `create_time`, `update_time`)
            VALUES
                (?, ?, ?, ?, ?)
        ';
    
        $bind = array(
            $good_spec_number_grade['good_spec_id'],
            $good_spec_number_grade['good_number_grade_id'],
            $good_spec_number_grade['price'],
            $good_info['update_time'],
            $good_info['update_time']
        );
    
        $stmt   = $write_db->query($sql, $bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
        if($result){
            $result = $write_db->last_insert_id();
        }
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function remove_attr_list($good_id)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
        
        $where_bind = array($good_id);
    
        $sql = '
            DELETE FROM `good_good_attr` 
            WHERE good_id = ? 
        ';
        //var_dump($sql); exit();
    
        $stmt   = $write_db->query($sql, $where_bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function get_spec_id_list($good_id)
    {
        $read_db = $this->_get_read_db();
    
        $where_bind = array($good_id);
    
        $sql = '
            SELECT id 
            FROM `good_good_spec`
            WHERE good_id = ? 
        ';
        //var_dump($sql); exit();
    
        $spec_id_list = $read_db->fetch_col($sql, $where_bind);
        //var_dump($spec_id_list); exit();
    
        return $spec_id_list;
    }
    
    public function remove_spec_sattr_list($spec_id_list)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $where_spec_id_list = $write_db->quote($spec_id_list);
    
        $sql = '
            DELETE FROM `good_good_spec_sattr`
            WHERE good_spec_id IN (' . $where_spec_id_list . ') 
        ';
        //var_dump($sql); exit();
    
        $stmt   = $write_db->query($sql);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function remove_spec_number_grade_list($spec_id_list)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $where_spec_id_list = $write_db->quote($spec_id_list);
    
        $sql = '
            DELETE FROM `good_good_spec_number_grade`
            WHERE good_spec_id IN (' . $where_spec_id_list . ')
        ';
        //var_dump($sql); exit();
    
        $stmt   = $write_db->query($sql);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function remove_spec_list($spec_id_list)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $where_spec_id_list = $write_db->quote($spec_id_list);
    
        $sql = '
            DELETE FROM `good_good_spec`
            WHERE id IN (' . $where_spec_id_list . ') 
        ';
        //var_dump($sql); exit();
    
        $stmt   = $write_db->query($sql);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function remove_sattr_list($good_id)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $where_bind = array($good_id);
    
        $sql = '
            DELETE FROM `good_good_sattr`
            WHERE good_id = ?
        ';
        //var_dump($sql); exit();
    
        $stmt   = $write_db->query($sql, $where_bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
    
    public function remove_number_grade_list($good_id)
    {
        $write_db = $this->_get_write_db();
        //var_dump($write_db); exit();
    
        $where_bind = array($good_id);
    
        $sql = '
            DELETE FROM `good_good_number_grade`
            WHERE good_id = ?
        ';
        //var_dump($sql); exit();
    
        $stmt   = $write_db->query($sql, $where_bind);
        $result = $stmt->rowCount();
        //var_dump($result); exit();
    
        return $result;
    }
}

